package com.cgbl.service.ui

import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.MemberFrameAdapter
import com.cgbl.service.assist.ProjectAssist
import com.cgbl.service.databinding.AppActivityMemberFrameBinding
import com.cgbl.service.vm.MemberFrameViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.MemberAdapterBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.arouter.ARouterUtil
import com.robot.base.utils.json.JSONParseUtils

@Route(path = AppConstants.Router.Project.A_MEMBER_FRAME)
class MemberFrameActivity : GLBaseActivity<AppActivityMemberFrameBinding, MemberFrameViewModel>() {

    lateinit var memberFrameAdapter: MemberFrameAdapter

    private val memberArray = arrayListOf<MemberAdapterBean>()

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_member_frame

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        memberFrameAdapter = MemberFrameAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = memberFrameAdapter
        }
        memberFrameAdapter.setOnItemClickListener { adapter, view, position ->
            val member = memberFrameAdapter.data[position]
            ARouterUtil.goToActivityWithBundle(AppConstants.Router.Project.A_MEMBER_FRAME_PROFILE,
                Bundle().apply {
                    putSerializable(AppConstants.BundleKey.MEMBER_BEAN, member)
                })
        }
        memberFrameAdapter.setNewInstance(memberArray)
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView?.text = "人员架构"
        backView?.setOnClickListener { finish() }
    }

    override fun initParam() {
        memberArray.addAll(ProjectAssist.getCurrentProjectMembers())
    }

    override fun initViewObservable() {
        LiveDataEventManager.observerRefreshProjectProfileEvent(this) {
            viewModel.refreshProjectProfile(ProjectAssist.getCurrentProjectId())
        }
        viewModel.uc.getMemberEvent.observe(this) {
            memberFrameAdapter.setNewInstance(it as MutableList<MemberAdapterBean>?)
        }
    }
}